//
//  ChoosePaymentViewController.m
//  HAODE
//
//  Created by panquanReabam.com on 15/9/8.
//  Copyright (c) 2015年 reabam.com. All rights reserved.
#import "RDVTabBarController.h"
#import "MyViewController.h"
#import "TheLoginViewController.h"
#import "MJRefresh.h"
#import "PayForSuccessViewController.h"
#import "ChoosePaymentViewController.h"
#import "ScanReceiptsViewController.h"
#import "OrderListViewController.h"
#import "HasSpecFmdbTool.h"
#import "NoSpecFmdbTool.h"
@interface ChoosePaymentViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate, UITextViewDelegate,UIImagePickerControllerDelegate,UIAlertViewDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate>

{
    MJRefreshHeaderView* _header;
    MJRefreshFooterView* _footer;
    NSArray *array;
    NSArray *arrayImage;
    UITableView*table;
    NSString*userToken;
    NSString *payType;
    NSString *payTypeName;
    NSString*payMsgCode;
    UIView*mcardPaView;
    UIView*mcardPaView1;
    UIButton*ToObtain;
    UITextField * mcardtext;
}
@end

@implementation ChoosePaymentViewController
@synthesize alt = alt;
-(void)BackAction:(UIButton *)sender{
  [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    self.navigationController.delegate = nil;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.view.backgroundColor=[UIColor whiteColor];
    [self setNavi];
}
-(void)viewDidLoad{
    [super viewDidLoad];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    userToken = [user objectForKey:@"tokenId"];
    if ([[self.mcardPay objectForKey:@"status"]isEqualToString:@"Y"] ) {
        arrayImage = @[@"xianjinzhifu",@"chuzhikazhifu" ,  @"shuakazhifu",@"weixinzhifu",@"zhifubaozhifu",@"shangchangdaishou",@"shaohouzhifu"];
        array=@[@"现金支付",@"储值卡支付",@"刷卡支付",@"微信支付",@"支付宝支付",@"商场代收",@"稍后支付"];

    }else{
    
        arrayImage = @[@"xianjinzhifu",@"shuakazhifu",@"weixinzhifu",@"zhifubaozhifu",@"shangchangdaishou",@"shaohouzhifu"];
        array=@[@"现金支付",@"刷卡支付",@"微信支付",@"支付宝支付",@"商场代收",@"稍后支付"];

    }
    
    table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-64) style:UITableViewStylePlain];
    table.delegate = self;
    table.dataSource = self;
    table.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:table];
    [table reloadData];
}

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([self.orderSat isEqualToString:@"CardTopUp"]) {
         return array.count-1;
    }
    return array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.font=[UIFont systemFontOfSize:15];
    cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[arrayImage objectAtIndex:indexPath.row]]];
    cell.textLabel.text = [array objectAtIndex:indexPath.row];
    cell.textLabel.textColor=[UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1];
   
    if ([[array objectAtIndex:indexPath.row] isEqualToString:@"储值卡支付"]) {
        NSString *s = [NSString stringWithFormat:@"¥%@可用",[self.mcardPay objectForKey:@"amount"]];
        UIFont *font = [UIFont systemFontOfSize:14];
        CGSize size = CGSizeMake(320, 2000);
        CGSize labelsize = [s sizeWithFont:font constrainedToSize:size lineBreakMode:NSLineBreakByCharWrapping];
        UILabel*isDefaults=[[UILabel alloc]initWithFrame:CGRectMake(160, 25,labelsize.width, 16)];
        isDefaults.text=s;
        isDefaults.backgroundColor=luseColor;
        isDefaults.textAlignment=NSTextAlignmentCenter;
        isDefaults.textColor=[UIColor whiteColor];
        isDefaults.font=[UIFont systemFontOfSize:11];
        [cell.contentView addSubview:isDefaults];
  
        
    }
    
    UIButton*jiantou = [[UIButton alloc]init];
    jiantou.frame = CGRectMake(ScreenWidth-40,17.5, 30, 30);
    jiantou.userInteractionEnabled=NO;
    [jiantou setImage:[UIImage imageNamed:@"shouqi"] forState:UIControlStateNormal];
    [cell.contentView addSubview:jiantou];
    if (array.count !=indexPath.row+1) {
        UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(10, 65-0.40, ScreenWidth-20, 0.40)];
        lineLabel1.backgroundColor=qianhuiColor;
        [cell.contentView addSubview:lineLabel1];
        
    }
    if (array.count ==indexPath.row+1) {
        UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(10, 65-0.40, ScreenWidth-20, 0.40)];
        lineLabel1.backgroundColor=qianhuiColor;
        [cell.contentView addSubview:lineLabel1];
        
    }
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UITableViewCell *cell =(UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    if ([cell.textLabel.text isEqualToString:@"现金支付"]) {
        payTypeName=@"现金支付";
        payType=@"CashPay";
        UIAlertView* alts = [[UIAlertView alloc] initWithTitle:@"确认支付" message:@"你选择了现金支付！" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
        [alts show];
      }else
    if ([cell.textLabel.text isEqualToString:@"刷卡支付"]) {
         payTypeName=@"刷卡支付";
        payType=@"CardPay";
        UIAlertView* alts = [[UIAlertView alloc] initWithTitle:@"确认支付" message:@"你选择了刷卡支付！" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
        [alts show];
}else
    if ([cell.textLabel.text isEqualToString:@"微信支付"]) {
        payType=@"WxScanPay";
        payTypeName=@"微信支付";
        if (self.Dict==nil) {//为空先提交下单／／不为空是直接补收银／／
            [self confirmOrder];
        }else{
            [self sweepPay];
        }
    }else
    if ([cell.textLabel.text isEqualToString:@"支付宝支付"]) {
        payType=@"AliQrPay";
        payTypeName=@"支付宝支付";
        if (self.Dict==nil) {
            [self confirmOrder];
        }else{
        [self sweepPay];
        }
    }else
    
    if ([cell.textLabel.text isEqualToString:@"商场代收"]) {
        payTypeName=@"商场代收";
        payType=@"MarketPay";
        UIAlertView* alts = [[UIAlertView alloc] initWithTitle:@"确认代收" message:@"你选择了商场代收！" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
        [alts show];
    }else

    if ([cell.textLabel.text isEqualToString:@"稍后支付"]) {
        payTypeName=@"稍后支付";
        if (self.Dict==nil) {
            [self confirmOrder];
        }else{
        OrderListViewController*de = [[OrderListViewController alloc] init];
        de.filterNPay=@"1";
        [self.navigationController pushViewController:de animated:YES];
        }
    }else
    if ([cell.textLabel.text isEqualToString:@"储值卡支付"]) {
            payTypeName=@"储值卡支付";
            payType=@"MCardPay";
        
        if ([[self.mcardPay objectForKey:@"isAvailable"]isEqualToString:@"Y"]) {
            if (self.Dict==nil) {
                
                [self confirmOrder];
                
            }else{
                
                [self MCardPay];
            }

        }else{
            [self showAlertMessage:@"余额不足！"];
        }
        }
}

-(void)cancel:(UIButton*)sender{
    
    OrderListViewController*de = [[OrderListViewController alloc] init];
    de.filterNPay=@"1";
    [self.navigationController pushViewController:de animated:YES];
    
    [mcardtext resignFirstResponder];
    [self.window makeKeyWindow];
    self.window.hidden=YES;
}

-(void)ture:(UIButton*)sender{//储值卡支付
    if (mcardtext.text.intValue==0) {
                [self showAlertMessage:@"请输入消费验证码！"];
    
            }else{
                NSMutableDictionary *params = [NSMutableDictionary new];
                [params setValue:userToken forKey:@"tokenId"];
                [params setValue:[self.Dict objectForKey:@"orderType"] forKey:@"orderType"];
                [params setValue:[self.Dict objectForKey:@"orderId"]  forKey:@"orderId"];
                [params setValue:mcardtext.text  forKey:@"payMsgCode"];
                [params setValue:[NSString stringWithFormat:@"%.2f",  [[self.Dict objectForKey:@"payAmount"]doubleValue]] forKey:@"payAmount"];
                [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.window];
                NSLog(@"-------------------%@",params);
                [HttpRequest requestData:[NSString stringWithFormat:@"Business/Pay/%@",payType] params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
                    if (isSuccess) {
                        NSLog(@"%@",severDataDict.dataDict);
                        if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                            [Global hideProgressHUDInternal:self.window];
                            [mcardtext resignFirstResponder];
                            [self.window makeKeyWindow];
                            self.window.hidden=YES;
                            PayForSuccessViewController *detailViewController = [[PayForSuccessViewController alloc] init];
                            detailViewController.orderId=[self.Dict objectForKey:@"orderId"];
                            detailViewController.payAmount=[NSString stringWithFormat:@"%.2f",  [[self.Dict objectForKey:@"payAmount"]doubleValue]];
                            detailViewController.memberId=self.memberId;
                            detailViewController.orderType=[self.Dict objectForKey:@"orderType"];
                            [self.navigationController pushViewController:detailViewController animated:YES];
                            
                            
                        }else{
                            [Global hideProgressHUDInternal:self.window];
                            NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                            [Global showTextHudSub:requestResultStr view:self.window];
                            [mcardtext resignFirstResponder];
                            [self.window makeKeyWindow];
                            self.window.hidden=YES;
                        }
                    }
                    else
                    {
                        
                        [Global hideProgressHUDInternal:self.window];
                        [Global showTextHudSub:severDataDict.resultStr view:self.window];
                        [mcardtext resignFirstResponder];
                        [self.window makeKeyWindow];
                        self.window.hidden=YES;
                    }
                    
                }];

                
    }
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        if (self.Dict==nil) {
            [self confirmOrder];
       
        }else{
            
            [self cashPay];
            
            
        }
    }else{
       
    
    }
}

-(void)confirmOrder{//提交订单
        [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
        [HttpRequest requestData:[NSString stringWithFormat:@"Business/%@"  ,self.orderSat] params:self.orderDict success:^(BOOL isSuccess, SeverData *severDataDict) {
            if (isSuccess) {
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {

                   [Global hideProgressHUDInternal:self.view];

                    self.Dict=severDataDict.dataDict;
                    if ([payTypeName isEqualToString: @"储值卡支付"]) {
                        [self MCardPay];
                    }else
                    if ([payTypeName isEqualToString: @"刷卡支付"]) {
                        [self cashPay];
                    }else
                    if ([payTypeName isEqualToString: @"商场代收"]) {
                      [self cashPay];
                    }else
                    if ([payTypeName isEqualToString: @"现金支付"]) {
                        [self cashPay];
                    }else if([payTypeName isEqualToString: @"支付宝支付"]){
                        [self sweepPay];
                    }else if([payTypeName isEqualToString: @"微信支付"]){
                        [self sweepPay];
                   
                    }else if([payTypeName isEqualToString: @"稍后支付"]){
                        OrderListViewController*de = [[OrderListViewController alloc] init];
                        de.chooseState=@"";
                        de.filterNPay=@"1";
                        [self.navigationController pushViewController:de animated:YES];
                }
                    NSString *sqlStr1=[NSString stringWithFormat:@"DELETE FROM t_NoSpec WHERE userToken = '%@' AND typeString  = '%@'",userToken,_saveTypeStr];
                    NSString *sqlStr2=[NSString stringWithFormat:@"DELETE FROM t_Spec WHERE userToken = '%@' AND typeString  = '%@'",userToken,_saveTypeStr];
                    [NoSpecFmdbTool deleteData:sqlStr1];
                    [HasSpecFmdbTool deleteData:sqlStr2];
                    
                }else{
                   [Global hideProgressHUDInternal:self.view];
                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    [Global showTextHudSub:requestResultStr view:self.view];
                    
                }
            }
            else
            {
                [Global hideProgressHUDInternal:self.view];
                [Global showTextHudSub:severDataDict.resultStr view:self.view];
            }
            
        }];
}
-(void)sweepPay{//扫码支付
    NSMutableDictionary *params = [NSMutableDictionary new];
    [params setValue:userToken forKey:@"tokenId"];
    [params setValue:[self.Dict objectForKey:@"orderType"] forKey:@"orderType"];
    [params setValue:[self.Dict objectForKey:@"orderId"]  forKey:@"orderId"];
    [params setValue:[NSString stringWithFormat:@"%.2f",  [[self.Dict objectForKey:@"payAmount"]doubleValue]] forKey:@"payAmount"];
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
    NSLog(@"%@",params);
    [HttpRequest requestData:[NSString stringWithFormat:@"Business/Pay/%@",payType] params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
         NSLog(@"%@",severDataDict.dataDict);
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                
                if ([[severDataDict.dataDict  objectForKey:@"qrCode"]length]>0) {
                    ScanReceiptsViewController  *generateVC=[[ScanReceiptsViewController alloc] init];
                    generateVC.payAmount=[self.Dict objectForKey:@"payAmount"];
                    generateVC.dic=severDataDict.dataDict;
                    generateVC.memberId=self.memberId;
                    [self.navigationController pushViewController:generateVC animated:YES];
  
                }else{
                
                    [self  sweepPayChargeToAccount:[severDataDict.dataDict objectForKey:@"gid"]];//扫描直接记账形式收款下单

                    
            }
           }else{
                [Global hideProgressHUDInternal:self.view];
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                [Global showTextHudSub:requestResultStr view:self.view];
            }
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            [Global showTextHudSub:severDataDict.resultStr view:self.view];
        }
        
    }];

}

-(void)sweepPayChargeToAccount :(NSString*)gidStr{//扫描直接记账形式收款下单
    
    NSMutableDictionary *params = [NSMutableDictionary new];
    [params setValue:userToken forKey:@"tokenId"];
    [params setValue:gidStr forKey:@"gid"];
    [HttpRequest requestData:@"Business/Gathering/Detail" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            NSLog(@"%@",severDataDict.dataDict);
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                PayForSuccessViewController *detailViewController = [[PayForSuccessViewController alloc] init];
                detailViewController.orderId=[self.Dict objectForKey:@"orderId"];
                detailViewController.payAmount=[NSString stringWithFormat:@"%.2f",  [[self.Dict objectForKey:@"payAmount"]doubleValue]];
                detailViewController.memberId=self.memberId;
                detailViewController.orderType=[self.Dict objectForKey:@"orderType"];
                [self.navigationController pushViewController:detailViewController animated:YES];
                
            }else{
                [Global hideProgressHUDInternal:self.view];
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                [Global showTextHudSub:requestResultStr view:self.view];
            }
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            [Global showTextHudSub:severDataDict.resultStr view:self.view];
        }}];
    
    
}
-(void)MCardPay{//储值卡支付
    
    
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.window makeKeyAndVisible];
    mcardPaView=[[UIView alloc]initWithFrame:CGRectMake(0,0, ScreenWidth, ScreenHeight)];
    mcardPaView.backgroundColor=[UIColor blackColor];
    mcardPaView.alpha=0.65;
    [self.window addSubview:mcardPaView];
    mcardPaView1=[[UIView alloc]initWithFrame:CGRectMake(30,200, ScreenWidth-60, 163)];
    mcardPaView1.backgroundColor=[UIColor whiteColor];
    [self.window addSubview:mcardPaView1];
    
    
    
    UILabel*phone=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth-60, 55)];
    
    if ([[self.mcardPay objectForKey:@"phone"]length]==11) {
        NSString *str = [[self.mcardPay objectForKey:@"phone"] stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
          phone.text=str;
    }else{
    
    }
    phone.textColor=qianheiColor;
    phone.textAlignment=NSTextAlignmentCenter;
    phone.font=[UIFont systemFontOfSize:15];
    [mcardPaView1 addSubview:phone];
    
    
    
    mcardtext = [[UITextField alloc]initWithFrame:CGRectMake(20, 60, ScreenWidth-90-100, 30)];
    mcardtext.placeholder = @"请输入会员消费的验证码"; //默认显示的字
    mcardtext.font=[UIFont systemFontOfSize:14];
    mcardtext.keyboardType = UIKeyboardTypeNumberPad;
    [mcardPaView1 addSubview:mcardtext];
    ToObtain = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth-60-90-20,55, 90,30)];
    [ToObtain setTitle:@"发送验证码" forState:UIControlStateNormal];
    ToObtain.titleLabel.font = [UIFont systemFontOfSize: 13.0];
    [ToObtain.layer setCornerRadius:1.0]; //设置矩圆角半径
    [ToObtain.layer setBorderWidth:0.5];   //边框宽度
    CGColorSpaceRef tureBTnSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef tureBTncolorref = CGColorCreate(tureBTnSpace,(CGFloat[]){228/255.0, 228/255.0, 228/255.0, 1.0});
    [ToObtain.layer setBorderColor:tureBTncolorref];//边框颜色
    [ToObtain setTitleColor:[UIColor colorWithRed:249/255.0 green:53/255.0 blue:30/255.0 alpha:1.0]
                   forState:UIControlStateNormal];
    [ToObtain addTarget:self action:@selector(startTime) forControlEvents:UIControlEventTouchUpInside];
    [mcardPaView1 addSubview:ToObtain];
    UIButton*cancel = [[UIButton alloc] initWithFrame:CGRectMake(0,113, (ScreenWidth-60)/2,50)];
    [cancel setTitle:@"取消" forState:UIControlStateNormal];
    [cancel setTitleColor:qianheiColor forState:UIControlStateNormal];
    cancel.titleLabel.font = [UIFont systemFontOfSize: 15.0];
    [cancel addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
    [mcardPaView1 addSubview:cancel];
    UIButton*ture = [[UIButton alloc] initWithFrame:CGRectMake((ScreenWidth-60)/2,113, (ScreenWidth-60)/2,50)];
    [ture setTitle:@"确认" forState:UIControlStateNormal];
    [ture setTitleColor:luseColor forState:UIControlStateNormal];
    ture.titleLabel.font = [UIFont systemFontOfSize: 15.0];
    [ture addTarget:self action:@selector(ture:) forControlEvents:UIControlEventTouchUpInside];
    [mcardPaView1 addSubview:ture];
    
    UILabel*line=[[UILabel alloc]initWithFrame:CGRectMake(0, 113, ScreenWidth-60, 0.45)];
    line.backgroundColor=qianhuiColor;
    [mcardPaView1 addSubview:line];
    UILabel*line1=[[UILabel alloc]initWithFrame:CGRectMake((ScreenWidth-60)/2, 113, 0.45 , 50)];
    line1.backgroundColor=qianhuiColor;
    [mcardPaView1 addSubview:line1];
}
-(void)cashPay{//现金支付和刷卡和代收

    NSMutableDictionary *params = [NSMutableDictionary new];
    [params setValue:userToken forKey:@"tokenId"];
    [params setValue:[self.Dict objectForKey:@"orderId"] forKey:@"orderId"];
    [params setValue:[self.Dict objectForKey:@"orderType"] forKey:@"orderType"];
    [params setValue:[NSString stringWithFormat:@"%.2f",  [[self.Dict objectForKey:@"payAmount"]doubleValue]] forKey:@"payAmount"];
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:20 view:self.view];
    NSLog(@"%@",params);
    [HttpRequest requestData:[NSString stringWithFormat:@"Business/Pay/%@",payType] params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            NSLog(@"%@",severDataDict.dataDict);
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                PayForSuccessViewController *detailViewController = [[PayForSuccessViewController alloc] init];
                detailViewController.orderId=[self.Dict objectForKey:@"orderId"];
                detailViewController.payAmount=[NSString stringWithFormat:@"%.2f",  [[self.Dict objectForKey:@"payAmount"]doubleValue]];
                detailViewController.memberId=self.memberId;
                detailViewController.orderType=[self.Dict objectForKey:@"orderType"];
                [self.navigationController pushViewController:detailViewController animated:YES];
                
            }else{
                [Global hideProgressHUDInternal:self.view];
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                [Global showTextHudSub:requestResultStr view:self.view];
            }
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            [Global showTextHudSub:severDataDict.resultStr view:self.view];
        }}];


}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  65;
}

- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}
- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}
-(void)setNavi{
    [[self rdv_tabBarController] setTabBarHidden:YES];
    self.navigationController.navigationBarHidden=NO;
    self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.font=[UIFont systemFontOfSize:13];
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    self.navigationController.delegate = self;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];
    
    if (self.Dict==nil) {
        if ([self.orderSat isEqualToString:@"GOrder/Add"] ) {
            self.title = [NSString stringWithFormat:@"¥ %.2f",  [[self.orderDict objectForKey:@"totalMoney"]doubleValue]] ;//后台设置的参数从订单界面带过来的
        }else{
            self.title = [NSString stringWithFormat:@"¥ %.2f",  [[self.orderDict objectForKey:@"realMoney"]doubleValue]] ;
        }
    }else{
    
      self.title = [NSString stringWithFormat:@"¥ %.2f",  [[self.Dict objectForKey:@"payAmount"]doubleValue]] ;//补收银已有的Dict带出来的
    }
   

   
    
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     backColor,
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];
    
}
-(void)startTime{
    
    __block int timeout=60; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [ToObtain setTitle:@"获取" forState:UIControlStateNormal];
                ToObtain.userInteractionEnabled = YES;
            });
        }else{
            //            int minutes = timeout / 60;
            int seconds = timeout % 60;
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                //                NSLog(@"____%@",strTime);
                if (strTime.intValue==59) {
                    NSLog(@"____%@",strTime);
                    NSMutableDictionary *params = [NSMutableDictionary new];
                    [params setValue:[self.mcardPay objectForKey:@"phone"] forKey:@"mobile"];
                    [params setValue:@"V" forKey:@"msgType"];
                     [params setValue:[self.Dict objectForKey:@"orderId"]  forKey:@"orderId"];
                    [HttpRequest requestData:@"System/SendMsg" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
                        if (isSuccess) {
                            NSLog(@"====%@",severDataDict.dataDict);
                            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                                [Global hideProgressHUDInternal:self.window];
                                //                                        NSString*ResultString=[severDataDict.dataDict objectForKey:@"ResultString"];
                                //                                        [self showAlertMessage:ResultString];
                                
                            }else{
                                [Global hideProgressHUDInternal:self.window];
                            }}
                        else
                        {
                            [Global hideProgressHUDInternal:self.window];
                            [Global showTextHudSub:severDataDict.resultStr view:self.window];
                        }
                    }];
                }
                
                if (![strTime isEqualToString:@"00"]) {
                    [ToObtain setTitle:[NSString stringWithFormat:@"%@秒重获",strTime] forState:UIControlStateNormal];
                    
                }
                ToObtain.userInteractionEnabled = NO;
                
            });
            timeout--;
            
        }
    });
    dispatch_resume(_timer);
}


@end
